//
//  ListTableViewController.swift
//  one
//
//  Created by huangfeng on 2018/5/3.
//  Copyright © 2018年 huangfeng. All rights reserved.
//

import UIKit

class ListTableViewController: UITableViewController {

    var listA  = [AddressPerson]()
    
    override func viewDidLoad() {

        super.viewDidLoad()
        listA = [AddressPerson]()
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        loadData { (list) in
            print(list)
            for person in list {
                    self.listA.append(person)
            }
            
            self.tableView.reloadData()
        }
    }
    
    
    
    
    ///模拟异步回调
    private func loadData(completion:@escaping (_ list: [AddressPerson]) ->()) ->() {
        
        DispatchQueue.global().async {
            print("正在努力加载数据")
            Thread.sleep(forTimeInterval: 1)
            var arrayM = [AddressPerson]()
            for i in 0..<20 {
                let p = AddressPerson()
                p.name = "zhangsan - \(i)"
                p.phone = "1860" + String(format: "%06d", arc4random_uniform(1000000))
                p.title = "boss"
                arrayM.append(p)
            }
            
            DispatchQueue.main.async( execute: {
                completion(arrayM)
            })
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func cancel_click(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }
    
//    // MARK: - Table view data source


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return listA.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath)

        let person: AddressPerson = listA[indexPath.row]
        cell.textLabel?.text = person.name
        cell.detailTextLabel?.text = person.phone
//         Configure the cell...

        return cell
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc = segue.destination as! ListDetail
        if let index = sender as? IndexPath {
            vc.person = listA[index.row]
            vc.completionCallBack = {
                self.tableView.reloadRows(at: [index], with: .automatic)
            }
        }else{
            vc.completionCallBack = { [weak vc] in
                guard let p = vc?.person else{
                    return
                }
                
                self.listA.insert(p, at: 0)
                self.tableView.reloadData()
                
            }
        }
    }
 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)

        performSegue(withIdentifier: "list2detail2", sender: indexPath)
        
    }

}
